//售价模板
angular.module('SmartCanteenApp.priceModule', ['ngRoute', 'ngTouch'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/priceModule', {
            templateUrl: 'base/priceModule.html',
            controller: 'priceModuleCtrl'
        });
    }])

    .controller('priceModuleCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
    			$scope.hideCanteen()
    			$(".modal-dialog").draggable();//为模态对话框添加拖拽
   				$(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动	
    			//分页清空
    			$scope.pagination([]);
    			$scope.initial = '10'; //设置默认每页条数
				//获取基础数据
			    $scope.showPrice = function(){
			    	//请求售价模板
                	$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/sales_mode','','get',function(data){
	    				$scope.prices = data.data
	    				$scope.pagination($scope.prices);
	    				if(data.data.length>0){
	    					$scope.logsL = false
	    				}else{
	    					$scope.logsL = true
	    				}
	    			})
                	//获取当前食堂下卡类型
					$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/sales_mode/cardTypes','','get',function(data){
    					console.log(data)
    					$scope.cards = data.data
  						$scope.card_types = $scope.cards
    				})
			    }
			   
		   		if( $scope.getCanteenId() != ''){
		   				$scope.canteenId =  $scope.getCanteenId() ;
		   				//获取基础数据
				 		$scope.showPrice();
		   		}
				$scope.$on('to-child', function(event,data) {
					//获取从父控制器传来的当前选中食堂
					$scope.canteenId = data;
					$scope.showPrice();
				});
                 //调用数据分页
//				$scope.pagination($scope.prices);
				$scope.priceMode = {}
				
    	
                //删除售价模块
                $scope.showPrice_del = function(id){
                	$scope.delId = id
                	$("#delPrice").modal('toggle')
                }
                $scope.del_price = function(){
                	console.log($scope.delId)  //delId为要删除的门店id
                	$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/sales_mode/'+$scope.delId,'','delete',function(data){
                		$("#delPrice").modal('toggle') //隐藏删除模态框
                		//重新请求列表内数据
                		$scope.showPrice()	
                	})
                	
                	
                }
               //新增售价模块
               
					console.log($scope.getId)

               
               //执行新增保存
               
    			$scope.saveNewPrice = function(){
    				$('#alertCartType').modal('toggle')
    				$scope.priceMode.card_types = $scope.card_types
    				var newPUrl = $scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/sales_mode'
    				console.log($scope.priceMode)
    				//提交验证姓名
    				if($scope.priceMode.name == '' || $scope.priceMode.name ==undefined){
    					$scope.showError('名称不能为空')
    					return
    				}
    				//提交验证卡类型
    				var keepGoing = true
    				angular.forEach($scope.priceMode.card_types, function(data){
						if(keepGoing){
							if(data.is_checked){
								keepGoing = false;
							}
						}
					});
					if(keepGoing){
						$scope.showError('请选择卡类型')
						return
					}
    				$scope.getJson(newPUrl,$scope.priceMode,'post',function(data){

     					if(data.code == 0){
     						$scope.priceMode.name = '' //清空对象
		    				for(var i=0;i<$scope.cards.length;i++){
		    					$scope.priceMode.card_types[i].is_checked = false  
		    				} //重置新增对象
		    				$scope.showSuccess()
	     					$("#newPrice").modal('toggle')//隐藏模态框
			    			$scope.showPrice() //重新请求数据		    				
     					}else{
     						$scope.showError(data.msg)
     					}
    				})
    			}
    			//取消新增售价模板
    			$scope.cancleNewPrice = function(){
    				$scope.priceMode = {}
					angular.forEach($scope.card_types, function(data){
						data.is_checked = false
					});
    			}
    			//售价模板查看
    			$scope.showprice_edit = function(id){
    				console.log(id)
    				var showPUrl = $scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/sales_mode/'+id
    				console.log(showPUrl)
    				$scope.getJson(showPUrl,'','get',function(data){
    					
//  					angular.forEach(data.data.items,function(data){
//  						data.mode_price = parseFloat(data.mode_price)
//  					})
    					$scope.priceDetails = data.data
    					
    					var arr = $scope.priceDetails.card_types
	    				var names = ''
	    				angular.forEach(arr, function(data){
							if(data.is_checked==false){
								names=names+data.name+','
							}
						});
						$scope.type_name = names
						$("#priceDetails").modal('toggle')
						console.log($scope.priceDetails)
    				})
    			}
    			//编辑卡类型取消还原ng-model
    			$scope.cancleType = function(id){
    				console.log(id)
    				var showPUrl = $scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/sales_mode/'+id
    				console.log(showPUrl)
    				$scope.getJson(showPUrl,'','get',function(data){
    					$scope.priceDetailsTemp = data.data
    					$scope.priceDetails.card_types = $scope.priceDetailsTemp.card_types
	    				$('#cardType').modal('toggle')
	    			})
    			}
    			
    			//显示模态框后调用更改
    			$('#priceDetails').on('shown.bs.modal', function (e) {
				  if($scope.priceDetails.mode_type == '2'){
    					$scope.baifenbi();
    				}
				})

    			//查看修改中按价格和按百分比显示模板价格、
    			$scope.baifenbi = function(){
    				var arr = $("input[name*='mode_price']")
    				angular.forEach(arr,function(data,i){
                          /*var name = $(data).next().attr("id")
                          $scope.priceModule.card_type_name[i] = name*/
                       var now_price =  $(data).parent().prev().html()
                       var mode_price = (now_price*$scope.priceDetails.discount/100).toFixed(2)
                       var cost = $(data).parent().next().html()
                       var maoli = $(data).parent().next().next().html((mode_price-cost).toFixed(2))  //计算毛利并写入
                       var maolilve = 1-cost/(mode_price-cost)
                      $(data).parent().next().next().next().html(maolilve.toFixed(2))  //计算毛利率并写入
                      /* (maoli-cost)/maoli*/
                       $(data).val(mode_price) //计算模板价格并写入
                    });
    			}
    			$scope.savePriceModule = function(id){
    				var modePriceArr = document.getElementsByClassName('mode_price')
    				angular.forEach(modePriceArr,function(data,i){
                       var modePrice = $(data).val()
	                      if(modePrice == '' || modePrice == null){
	                      	$scope.showError('模板价格不能为空')
	                      	return
	                      }
                       $scope.priceDetails.items[i].mode_price = modePrice
                    });
    				console.log(this.priceDetails)
    				//提交验证卡类型
    				var keepGoing = true
    				angular.forEach($scope.priceDetails.card_types, function(data){
						if(keepGoing){
							if(data.is_checked){
								keepGoing = false;
							}
						}
					});
					if(keepGoing){
						$scope.showError('请选择卡类型')
						return
					}
    				var peiceUrl = $scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/sales_mode/'+id
					$scope.getJson(peiceUrl,this.priceDetails,'put',function(data){
						if(data.code == 0){
							$("#priceDetails").modal('toggle')
							$scope.showPrice()								
						}else{
							$scope.showError(data.msg)
						}
					})
					
    		}
    			//修改中卡类型选择显示
    			$scope.showCardType = function(){
    				$('#cardType').modal('toggle')
    			}
    	//获取选中的cardtype
    	$scope.getCardTypes = function(){
    		console.log(this.cardTypes)
    		$('#cardType').modal('toggle')
    	}
    	$scope.change = function(price,index){
//  		if(parseInt(price)>1000){
//				$scope.showError('价格不能大于1000')
//				$scope.priceDetails.items[index].mode_price = ''
//				return
//			}
//			if( !isNumber(price)){
//				$scope.priceDetails.items[index].mode_price = ''
//				return
//			}
			console.log(price)
			if( checkDouble(price) && (price + '')!= '' && (price + '')!= 'null' && (price + '')!= null&& (price + '')!= 'undefined'){
				$scope.showError('只能输入小数位数最长2位的正数');
				$scope.priceDetails.items[index].mode_price = '1';
			}

    		var maoliarr = document.getElementsByClassName('maoli')
			angular.forEach(maoliarr,function(data,i){
                          /*var name = $(data).next().attr("id")
                          $scope.priceModule.card_type_name[i] = name*/
                       var chengben = $(data).prev().html()
                       var mubanjiage = $(data).prev().prev().children().val()
                       $(data).html((mubanjiage-chengben).toFixed(2))
                       $(data).next().html(((mubanjiage-chengben*2)/(mubanjiage-chengben)).toFixed(2))
           });           
    	}

    }])
